<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">所搜关键字</label>
                            <div class="layui-input-inline">
                                <input type="text" id="keyword" name="keyword" autocomplete="off" class="layui-input"
                                       placeholder="请输入搜索关键字">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script>
    layui.use(['form', 'table', 'miniPage', 'element', 'laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            laydate = layui.laydate,
            miniPage = layui.miniPage;

        table.render({
            //渲染的表格对应ID
            elem: '#currentTableId',
            url: '/records/getRecordByParam',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            //表头
            cols: [
                [
                    {type: "checkbox", width: 60},
                    {field: 'recId', width: 80, title: '编号', sort: true},
                    {field: 'recTime', width: 180, title: '登记时间', sort: true},
                    {
                        field: 'recType', width: 110, title: '登记类型', sort: true,
                        templet: function (res) { //0出去 1进来
                            if (res.recType == '0') {
                                return '<span>出去</span>';
                            } else {
                                return '<span>进来</span>';
                            }
                        }
                    },
                    {field: 'recPersonId', width: 0, title: '登记人编号'},
                    {
                        field: 'recPerType', width: 110, title: '登记类型', sort: true,
                        templet: function (res) {//住户是0 访客是1
                            if (res.recPerType == '0') {
                                return '<span class="layui-btn  layui-btn-xs">住户</span>'
                            } else {
                                return '<span class="layui-btn layui-btn-normal layui-btn-xs">访客</span>'
                            }
                        }
                    },
                    {field: 'visName', width: 100, title: '姓名'},
                    {field: 'visPhone', width: 120, title: '联系方式'},
                    {field: 'recOrderadd', width: 110, title: '来源地'},
                    {field: 'recTempera', width: 80, title: '体温', sort: true},
                    {field: 'recRemark', width: 150, title: '备注', sort: true},
                    {field: 'addId', width: 90, title: '受访地址'},
                    {title: '操作', minWidth: 130, toolbar: '#currentTableBar', align: "center"}
                ]
            ],
            limits: [10, 15, 20, 25, 50, 100],//每页条数的选择项
            curr: 1,
            limit: 10,//每页显示的条数
            page: true,//开启分页
            skin: 'line',//表格的风格，外观
            parseData: function (res) {
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "count": res.count,
                    "data": res.data.records,
                };
            }, done: function () {
                //隐藏不需要的列
                $("[data-field='recPersonId']").css('display', 'none');
            }

        });


        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    params: result
                }
            }, 'data');
            return false;
        });

        /**
         * toolbar事件监听
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {   // 监听添加操作
                var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '添加登记信息',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['50%', '90%'],
                    offset: 'auto',
                    content: '',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作,获取表格中选中的行数据
                var checkStatus = table.checkStatus('currentTableId');
                // 获取被选中记录对应的数据
                var saleChanceData = checkStatus.data;
                if (saleChanceData.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 2});
                    return;
                }

                layer.confirm('确定删除选中数据？', {icon: 3, title: "登记信息管理"}, function (index) {
                    //关闭确认框
                    layer.close(index);
                    //传递的参数是数组，获取传递的ID
                    var ids = "ids=";
                    //循环获取选中的行的数据
                    for (var i = 0; i < saleChanceData.length; i++) {
                        if (i < saleChanceData.length - 1) {
                            ids = ids + saleChanceData[i].recId + "&ids="
                        } else {
                            ids = ids + saleChanceData[i].recId
                        }
                    }
                    //向服务器发送删除指令  多个删除
                    $.ajax({
                        url: '/records/deleteRecords',
                        type: 'POST',
                        data: ids,
                        success: function (suc) {
                            if (suc.code == '200') {
                                layer.msg("删除记录成功！", {icon: 6, time: 1000});
                                parent.window.location.reload();
                            } else {
                                layer.msg("删除登记信息失败！", {icon: 5, time: 1000});
                            }
                        }
                    });
                });


            }
        });


        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            // console.log(obj)
        });


        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                //使用弹出层
                layer.open({
                    title: '修改信息',
                    type: 1,
                    shade: 0.0,
                    maxmin: true,
                    shadeClose: true,
                    area: ['50%', '80%'],
                    offset: ['100px', '20%'],
                    content: $("#updateRec")
                });
                if (data.recPerType == 0) {
                    document.getElementById("addHouse").style.display = "none";
                    document.getElementById("addUnit").style.display = "none";
                    setFormValue(obj, data);
                } else {
                    setFormValue(obj, data);
                }
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定删除选中数据？', {icon: 3, title: "登记信息管理"}, function (index) {
                    //关闭确认框
                    layer.close(index);
                    //向服务器发送删除指令  单个删除
                    $.ajax({
                        url: '/records/deleteRecords',
                        type: 'POST',
                        data: {
                            'ids': obj.data.recId
                        },
                        success: function (suc) {
                            if (suc.code == '200') {
                                layer.msg("删除登记信息成功！", {icon: 6, time: 1000});
                                parent.window.location.reload();
                            } else {
                                layer.msg("删除登记失败！", {icon: 5, time: 1000});
                            }
                        }
                    })
                });

            }
        });

        laydate.render({
                elem: '#recTime',
                type: 'datetime',
                format: 'yyyy-MM-dd HH:mm:ss',
                value: new Date(),
                min: '2000-01-01 00:00:00',
            }
        );

        //更改用户信息
        function setFormValue(obj, data) {
            var into = document.getElementById("in");
            var out = document.getElementById("out");
            if (data.recType == 0) {
                // 0 出去
                out.setAttribute("checked", true);
            } else {
                // 1 进入
                into.setAttribute("checked", true);
            }
            var vis = document.getElementById("vis");
            var res = document.getElementById("res");
            if (data.recPerType == 0) {
                //住户
                res.setAttribute("checked", true);
            } else if (data.recPerType == 1) {
                //访客
                vis.setAttribute("checked", true);
            }
            form.val("updateBtn", {
                "recId": data.recId,
                "recTime": data.recTime,
                "recPersonId": data.recPersonId,
                "visName": data.visName,
                "resId": data.resId,
                "visPhone": data.visPhone,
                "recOrderadd": data.recOrderadd,
                "recTempera": data.recTempera,
                "recRemark": data.recRemark,
            });
        }

        form.on('submit(updateBtn)', function (data) {
            var datas = data.field;
            if (datas.recType == '出') {
                datas.recType = 0;
            } else {
                datas.recType = 1;
            }
            if (datas.recPerType == '住户') {
                datas.recPerType = 0;
            } else {
                datas.recPerType = 1;
            }

            $.ajax({
                url: '/records/updateRecords',
                type: 'post',
                data: datas,
                dataType: "json",
                success: function (res) {
                    if (res.code == '200') {
                        layer.msg("修改成功！", {
                            icon: 6,
                            time: 300
                        }, function () {
                            parent.window.location.reload();//刷新页面
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex)
                        });
                    } else {
                        layer.msg("修改失败！", {icon: 2});
                    }
                }, error: function (data) {
                    layer.close(loadIndex);
                    layer.msg(data.responseJSON.msg, {icon: 2});
                }
            });
            return false;
        });


    });

</script>
<div class="layui-form layuimini-form" id="updateRec" style="display: none" lay-filter="updateBtn">

    <div class="layui-form-item">
        <label class="layui-form-label required">登记编号</label>
        <div class="layui-input-block">
            <input type="text" readonly="readonly" name="recId" id="recId" lay-verify="required" placeholder="登记编号:"
                   autocomplete="off" class="layui-input">
            <tip>登记编号不可修改！</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">进出时间</label>
        <div class="layui-input-block">
            <input type="text" name="recTime" id="recTime" lay-verify="required"
                   placeholder="进出时间:  yyyy-MM-dd HH:mm:ss" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">进出类型</label>
        <div class="layui-input-block">
            <input type="radio" name="recType" id="in" value="进" title="进">
            <input type="radio" name="recType" id="out" value="出" title="出">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">访客姓名</label>
        <div class="layui-input-block">
            <input type="text" name="visName" lay-verify="required" lay-reqtext="姓名不能为空" placeholder="请输入姓名" value=""
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">联系方式</label>
        <div class="layui-input-block">
            <input type="text" name="visPhone" lay-verify="required|phone" lay-reqtext="手机号不能为空" placeholder="请输入联系方式"
                   value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" id="resId">
        <label class="layui-form-label required">受访住户编号</label>
        <div class="layui-input-block">
            <input type="text" name="resId" lay-reqtext="受访住户编号不能为空" placeholder="请输入受访住户编号" value=""
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required ">登记类型</label>
        <div class="layui-input-block">
            <input type="radio" name="recPerType" id="vis" value="访客" title="访客" disabled>
            <input type="radio" name="recPerType" id="res" value="住户" title="住户" disabled>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">来源地</label>
        <div class="layui-input-block">
            <input type="text" name="recOrderadd" lay-verify="required" lay-reqtext="来源地不能为空" placeholder="请输入从什么地方来"
                   value="" class="layui-input">
            <tip>从何处来</tip>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">体温</label>
        <div class="layui-input-block">
            <input type="text" name="recTempera" placeholder="请输入进出人体温,精确到小数点后一位 如：36.5" value="36.5"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">备注信息</label>
        <div class="layui-input-block">
            <input type="text" name="recRemark" placeholder="备注信息" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <input type="hidden" name="recPersonId">
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="updateBtn">确认更新</button>
        </div>
    </div>
</div>